package com.cdtu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdtu.entity.*;
import com.cdtu.service.SysDeptService;
import com.cdtu.service.SysEmployeeService;
import com.cdtu.service.SysSalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/salary")
//解决跨域问题
@CrossOrigin
public class SysSalaryController extends BaseController {

    @Autowired
    private SysSalaryService sysSalaryService;

    @Autowired
    private SysEmployeeService sysEmployeeService;

    @Autowired
    private SysDeptService sysDeptService;

    @RequestMapping("/list")
    public ModelAndView salaryList(ModelAndView modelAndView){
        List<SysSalary> list = sysSalaryService.list();
        modelAndView.addObject("salaryList",list);

        List<SysDept> list1 = sysDeptService.list();
        modelAndView.addObject("deptList",list1);

        modelAndView.setViewName("system/sysSalary/salaryList");
        return modelAndView;
    }

    @RequestMapping("/delete/{id}")
    public ModelAndView delete(@PathVariable("id") Long id, ModelAndView modelAndView) {

        boolean remove = sysSalaryService.removeById(id);
        modelAndView.setViewName("redirect:/salary/list");
        return modelAndView;
    }

    @RequestMapping("/toAddUI")
    public ModelAndView toAddSysSalary(ModelAndView modelAndView) {

        List<SysEmployee> list = sysEmployeeService.list();
        //返回数据
        modelAndView.addObject("empList", list);



        //2.跳转页面;
        modelAndView.setViewName("system/sysSalary/addSysSalary");
        return modelAndView;
    }

    @RequestMapping("/addSalary")
    public ModelAndView addEmpUI(SysSalary sysSalary, ModelAndView modelAndView) {

        String empName = sysSalary.getEmpName();

        SysEmployee employeeByUsername = sysEmployeeService.getEmployeeByUsername(empName);
        sysSalary.setDeptId(employeeByUsername.getDeptId());

        sysSalary.setCreated(new Date());

        sysSalaryService.save(sysSalary);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/salary/list");
        return modelAndView;
    }


    @RequestMapping("/toUpdateUI/{id}")
    public ModelAndView toUpdateUI(ModelAndView modelAndView,@PathVariable("id") Long id) {

        SysSalary sysSalary = sysSalaryService.getById(id);
        //返回数据
        modelAndView.addObject("sysSalary", sysSalary);

        //2.跳转页面;
        modelAndView.setViewName("system/sysSalary/updateSysSalary");
        return modelAndView;
    }
    @RequestMapping("/update")
    public ModelAndView update(SysSalary sysSalary,ModelAndView modelAndView){

        sysSalaryService.updateById(sysSalary);


        modelAndView.setViewName("redirect:/salary/list");
        return modelAndView;
    }

}
